package club.justwill.single.persistence.dao;

import club.justwill.single.persistence.jpo.Audit;
import club.justwill.single.persistence.support.BaseDao;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 * Created by jaylon on 16-1-20.
 */
@Repository
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class AuditDao extends BaseDao<Audit> {

    public Audit getByProjectId(long projectId) {
        Query query = em.createNamedQuery("Audit.getByProjectId").setParameter("projectId", projectId);
        try {
            return (Audit)query.getSingleResult();
        } catch (NonUniqueResultException e) {
            return (Audit)query.getResultList().get(0);
        } catch (NoResultException e) {
            return null;
        }
    }
}
